﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Data.SqlClient;
using System.Data.EntityClient;
using VLUSM_BusinessLogic.Helpers;
using VLUSM_DataAccess;
using VLUSM_DataAccess.Repositories;
using VLUSM_BusinessLogic.DataTransferObjects;

namespace VLUSM_BusinessLogic.UserAccount
{
    public class UserAccountManager
    {
        IUnityContainer container = new UnityContainer();

        SqlConnectionStringBuilder sqlBuilder;
        EntityConnectionStringBuilder DiemBuilder = new EntityConnectionStringBuilder();
        EntityConnectionStringBuilder MainDataBuilder = new EntityConnectionStringBuilder();

        public UserAccountManager()
        {
            this.UnityRegisterType();
            this.InitializeMapper();

            this.sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = @".\SQLEXPRESS";
            sqlBuilder.InitialCatalog = "VLUSM";
            sqlBuilder.IntegratedSecurity = false;
            sqlBuilder.MultipleActiveResultSets = true;
            sqlBuilder.PersistSecurityInfo = true;
            sqlBuilder.UserID = "sa";
            sqlBuilder.Password = "123";

            #region Generate Entity Connection
            DiemBuilder.ProviderConnectionString = sqlBuilder.ToString();
            DiemBuilder.Provider = "System.Data.SqlClient";
            DiemBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "Diem");

            MainDataBuilder.ProviderConnectionString = sqlBuilder.ToString();
            MainDataBuilder.Provider = "System.Data.SqlClient";
            MainDataBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "MainData");
            #endregion
        }

        private void UnityRegisterType()
        {
            container.RegisterType<UserRepository>(new InjectionConstructor(MainDataBuilder.ConnectionString));
        }

        private void InitializeMapper()
        {
            Converter.ConfigureUserMapping();
        }

        public IEnumerable<UserDTO> GetUserList()
        {
            using (var repo = container.Resolve<UserRepository>(new ParameterOverride("connectionString", MainDataBuilder.ConnectionString)))
            {
                var result = repo.GetAll();
                return Converter.ConvertToUserDTOList(result);
            }
        }

        public IEnumerable<UserDTO> GetUserListByDonViID(int DonViIDFilter)
        {
            using (var repo = container.Resolve<UserRepository>(new ParameterOverride("connectionString", MainDataBuilder.ConnectionString)))
            {
                var result = repo.GetAllByDonViID(DonViIDFilter);
                return Converter.ConvertToUserDTOList(result);
            }
        }

        public UserDTO GetUserByID(int IDFilter)
        {
            using (var repo = container.Resolve<UserRepository>(new ParameterOverride("connectionString", MainDataBuilder.ConnectionString)))
            {
                var result = repo.GetSingleByID(IDFilter);
                return Converter.ConvertToUserDTO(result);
            }
        }

        public UserDTO GetUserByTenTaiKhoan(string TenTaiKhoanFilter)
        {
            using (var repo = container.Resolve<UserRepository>(new ParameterOverride("connectionString", MainDataBuilder.ConnectionString)))
            {
                var result = repo.GetSingleByTenTaiKhoan(TenTaiKhoanFilter);
                return Converter.ConvertToUserDTO(result);
            }
        }

        public UserDTO GetUserByUserNamePassword(string username, string password)
        {
            using (var repo = container.Resolve<UserRepository>(new ParameterOverride("connectionString", MainDataBuilder.ConnectionString)))
            {
                var result = repo.GetSingleByUserNamePassword(username, password);
                return Converter.ConvertToUserDTO(result);
            }
        }

        public bool Validate(string username, string password)
        {
            var query = this.GetUserByUserNamePassword(username.Trim(), password.Trim());
            if (query == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
